import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:ichinesereader/constants.dart';
import 'package:ichinesereader/controller/evaluation.dart';
import 'package:ichinesereader/models/evaluation/evaluationBookReportResult.dart';
import 'package:ichinesereader/models/evaluation/evaluationClassReportResult.dart';
import 'package:ichinesereader/utils/helper.dart';
import 'package:ichinesereader/utils/log.dart';
import 'package:ichinesereader/widgets/grids/evaluationClassReportDataGrid.dart';
import 'package:ichinesereader/widgets/layouts/customAppBar.dart';
import 'package:syncfusion_flutter_gauges/gauges.dart';
import '../../widgets/buttons/floatingActionButtonsTeacherSwitchClass.dart';

// ignore: must_be_immutable
class EvaluationBookReportForTeacher extends GetView<EvaluationController> {
  EvaluationBookReportForTeacher(
      {Key? key, required this.bookTitle, required this.type})
      : super(key: key);
  String? bookTitle;
  String? type;

  @override
  Widget build(BuildContext context) {
    Log.debug('build teacher evaluation book report');

    return Scaffold(
      appBar: CustomAppBar(title: this.bookTitle.toString()),
      floatingActionButton: FloatingActionButtonsTeacherSwitchClass(),
      body: Padding(
        padding: const EdgeInsets.all(8.0),
        child: Center(
          child: Column(
            children: [
              Obx(() {
                if (controller.evaluationBookReportResult.value.result !=
                        null &&
                    type != "AP" &&
                    type != "SOCIAL_STUDIES") {
                  return _buildSummaryGauges(
                      controller.evaluationBookReportResult.value);
                } else {
                  return Text('');
                }
              }),
              Expanded(
                child: controller.obx(
                  (data) {
                    List<EvaluationBookReport> bookdata = [];
                    if (controller.evaluationBookReportResult.value.result !=
                        null) {
                      bookdata = controller.evaluationBookReportResult.value
                          .result?.list as List<EvaluationBookReport>;
                    }
                    return EvaluationClassReportDataGrid(bookdata, true);
                  },
                  onEmpty: Text('Data not available ...'.tr),
                  // onLoading: Center(
                  //     child: CircularProgressIndicator(
                  //   backgroundColor: Get.theme.primaryColorDark,
                  //   strokeWidth: 9.0,
                  // )),
                ),
              ),
            ],
          ),
        ),
      ),
    );
  }

  Wrap _buildSummaryGauges(EvaluationBookReportResult resultData) {
    List<Widget> _widgets = [];
    var totalStudents = resultData.result?.list?.length;
    var summaryData = resultData.result?.summary;
    var gauge = SfRadialGauge(
      enableLoadingAnimation: true,
      animationDuration: 2500,
      axes: <RadialAxis>[
        RadialAxis(
            //labelsPosition: ElementsPosition.outside,
            axisLineStyle: const AxisLineStyle(
                thicknessUnit: GaugeSizeUnit.factor, thickness: 0.2),
            radiusFactor: 0.8,
            showTicks: false,
            showFirstLabel: false,
            interval: totalStudents!.toDouble(),
            showLastLabel: true,
            maximum: totalStudents.toDouble(),
            axisLabelStyle: const GaugeTextStyle(),
            annotations: <GaugeAnnotation>[
              GaugeAnnotation(
                  angle: 180,
                  widget: Row(
                    mainAxisSize: MainAxisSize.min,
                    children: <Widget>[
                      Text(
                        'Completed'.tr,
                        style: TextStyle(
                            fontFamily: 'Times',
                            fontSize: 12,
                            fontWeight: FontWeight.w400,
                            fontStyle: FontStyle.italic),
                      ),
                    ],
                  )),
            ],
            pointers: <GaugePointer>[
              NeedlePointer(
                  enableAnimation: true,
                  gradient: const LinearGradient(colors: <Color>[
                    Color.fromRGBO(203, 126, 223, 0),
                    Color(0xFFCB7EDF)
                  ], stops: <double>[
                    0.25,
                    0.75
                  ], begin: Alignment.bottomCenter, end: Alignment.topCenter),
                  animationType: AnimationType.easeOutBack,
                  value: summaryData!.completedCount!.toDouble(),
                  animationDuration: 1300,
                  needleStartWidth: 2,
                  needleEndWidth: 4,
                  needleLength: 0.7,
                  knobStyle: const KnobStyle(
                    knobRadius: 0,
                  )),
              RangePointer(
                  value: summaryData.completedCount!.toDouble(),
                  width: 0.2,
                  sizeUnit: GaugeSizeUnit.factor,
                  color: const Color(0xFF494CA2),
                  animationDuration: 1300,
                  animationType: AnimationType.easeOutBack,
                  gradient: const SweepGradient(
                      colors: <Color>[Color(0xFF9E40DC), Color(0xFFE63B86)],
                      stops: <double>[0.25, 0.75]),
                  enableAnimation: true)
            ])
      ],
    );

    var container = Container(
      width: 150,
      height: 150,
      child: gauge,
    );
    _widgets.add(container);

    Constants.evaluationSkillTypes.forEach((skill) {
      double maxValue = 10;
      double currentValue = 0;
      Color sColor = hexStringToHexInt(skill.color.toString());

      if (skill.skill == 'Listening') {
        currentValue = (summaryData.listeningAccuracy! / 10).toDouble();
      }
      if (skill.skill == 'Reading') {
        currentValue = (summaryData.readingAccuracy! / 10).toDouble();
      }
      if (skill.skill == 'Speaking') {
        maxValue = 10;
        currentValue = summaryData.speakingScore!.toDouble();
      }
      if (skill.skill == 'Writing') {
        maxValue = 10;
        currentValue = summaryData.writingScore!.toDouble();
      }
      if (skill.skill == 'AI Speaking') {
        currentValue = (summaryData.speakingAiScore! / 10).toDouble();
      }

      gauge = SfRadialGauge(
        enableLoadingAnimation: true,
        animationDuration: 2500,
        axes: <RadialAxis>[
          RadialAxis(
              //labelsPosition: ElementsPosition.outside,
              axisLineStyle: const AxisLineStyle(
                  thicknessUnit: GaugeSizeUnit.factor, thickness: 0.2),
              radiusFactor: 0.8,
              showTicks: false,
              interval: 1,
              showFirstLabel: false,
              showLastLabel: true,
              maximum: maxValue,
              axisLabelStyle: const GaugeTextStyle(),
              annotations: <GaugeAnnotation>[
                GaugeAnnotation(
                    angle: 180,
                    widget: Row(
                      mainAxisSize: MainAxisSize.min,
                      children: <Widget>[
                        Image.asset(
                          skill.image.toString(),
                          height: 36,
                          width: 36,
                        ),
                        // Text(
                        //   '${skill.skill}',
                        //   style: TextStyle(
                        //       fontFamily: 'Times',
                        //       fontSize: 14,
                        //       fontWeight: FontWeight.w400,
                        //       fontStyle: FontStyle.italic),
                        // ),
                      ],
                    )),
              ],
              pointers: <GaugePointer>[
                NeedlePointer(
                    enableAnimation: true,
                    gradient: const LinearGradient(colors: <Color>[
                      Color.fromRGBO(203, 126, 223, 0),
                      Color(0xFFCB7EDF)
                    ], stops: <double>[
                      0.25,
                      0.75
                    ], begin: Alignment.bottomCenter, end: Alignment.topCenter),
                    animationType: AnimationType.easeOutBack,
                    value: currentValue,
                    animationDuration: 1300,
                    needleStartWidth: 2,
                    needleEndWidth: 4,
                    needleLength: 0.7,
                    knobStyle: const KnobStyle(
                      knobRadius: 0,
                    )),
                RangePointer(
                    value: currentValue,
                    width: 0.20,
                    sizeUnit: GaugeSizeUnit.factor,
                    color: sColor,
                    animationDuration: 1300,
                    animationType: AnimationType.easeOutBack,
                    enableAnimation: true)
              ])
        ],
      );
      container = Container(
        width: 150,
        height: 150,
        child: gauge,
      );
      _widgets.add(container);
    });

    return Wrap(
      //mainAxisAlignment: MainAxisAlignment.center,
      alignment: WrapAlignment.start,
      crossAxisAlignment: WrapCrossAlignment.start,
      children: _widgets,
    );
  }
}
